using System;
using System.Collections.Generic;
using iPhOwa.Exchange.ContentClasses;
using iPhOwa.Exchange.Managers;

namespace iPhOwa.Web.Controls
{
	/// <summary>
	/// Contacts list.
	/// </summary>
	public partial class ContactsContent : iPhOwaControl
	{
		/// <summary>
		/// Load event.
		/// </summary>
		protected void Page_Load(object sender, EventArgs e)
		{
			Folder currentFolder = FolderManager.Get(Path, OwaSession);

			if(currentFolder != null)
			{
				if(Session[Constants.ContactsCacheName] == null)
				{
					Session[Constants.ContactsCacheName] = new List<Contact>(ContactManager.GetList(Path, OwaSession));
				}

				var contacts = (List<Contact>)Session[Constants.ContactsCacheName];

				if(contacts != null && contacts.Count > 0)
				{
					contacts.Sort(delegate(Contact x, Contact y)
					              	{
					              		string xVal = x.IsCompany ? x.Company : x.DisplayName;
					              		string yVal = y.IsCompany ? y.Company : y.DisplayName;

					              		return string.Compare(xVal, yVal, StringComparison.CurrentCulture);
					              	});

					nl.DataSource = contacts;
					nl.DataBind();
				}
				else
				{
					nl.Visible = false;
					np.Visible = true;
				}
			}
		}
	}
}